import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:imperius_insights/portal/env/environments.dart';
import 'package:imperius_insights/portal/state/mobx/store.dart';
import 'package:imperius_insights/portal/widgets/toast.dart';
import 'package:imperius_insights/routes/navigator.dart';
import 'package:imperius_insights/routes/paths.dart';
import 'package:imperius_insights/treaty/strings.dart';
import 'package:mobx/mobx.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart';

part 'view_model.g.dart';

class ViewModel = _ViewModel with _$ViewModel;

abstract class _ViewModel with Store, StoreWithLifecycle {
  @observable
  ObservableList<FeatureUnit> features;
  @observable
  String versionText;

  _ViewModel() {
    versionText = '评估版(20200617#lastest)';
    features = ObservableList.of([
      FeatureUnit(Strings.toolbox_select_department,
          icon: Icons.people,
          routePath: Paths.scopeDepartments,
          statsName: 'toolbox_select_department_humans'),
      FeatureUnit(Strings.toolbox_select_humans,
          icon: Icons.person,
          routePath: Paths.scopeHumans,
          statsName: 'toolbox_select_department_humans'),
      FeatureUnit(Strings.project_tasks_calendar, icon: Icons.date_range, clickHandler: () => navigateTo(
          context, Paths.projectsCalendar)),
      FeatureUnit(Strings.toolbox_network_title,
          icon: Icons.bug_report,
          routePath: Paths.diagnostic,
          statsName: 'toolbox_diagnostic'),
      FeatureUnit(Strings.toolbox_links_releases_client,
          icon: Icons.schedule,
          schemas: ['http://mportal.corp.qunar.com/client/10010'],
          statsName: 'toolbox_links_releases_client'),
      FeatureUnit(Strings.toolbox_links_libra,
          icon: Icons.data_usage,
          schemas: ['http://qlibra.corp.qunar.com/eventAnalysis'],
          statsName: 'toolbox_links_libra'),
      FeatureUnit(Strings.toolbox_links_performance_flows,
          icon: Icons.show_chart,
          schemas: ['http://watcher.corp.qunar.com/#/dash/team/qunar.team.flight.userproduct.f_haders_dashboard.performance.domestic'],
          statsName: 'toolbox_links_performance_flows'),
      FeatureUnit(Strings.toolbox_links_myth,
          icon: Icons.important_devices,
          schemas: ['http://myth.qunar.com/'],
          statsName: 'toolbox_links_myth'),
      FeatureUnit(Strings.toolbox_links_stats_distribute,
          icon: Icons.pie_chart,
          schemas: ['http://apm.corp.qunar.com/qpStat/version-stat?pid=10010&platform=adr&hybridid=f_major_bundle_rn_android'],
          statsName: 'toolbox_links_stats_distribute'),
      FeatureUnit(Strings.toolbox_links_tars_jenkins,
          icon: Icons.adb,
          schemas: ['http://qa.corp.qunar.com/jenkins/'],
          statsName: 'toolbox_links_tars_jenkins'),
      FeatureUnit(Strings.toolbox_links_project_jeeves,
          icon: Icons.remove_red_eye,
          schemas: ['https://rankqr.com/jeeves'],
          statsName: 'toolbox_links_jeeves'),
      FeatureUnit(Strings.toolbox_links_project_plans,
          icon: Icons.calendar_today,
          schemas: ['http://qa.corp.qunar.com/qodin/taskplan.html?team=%E6%9C%BA%E7%A5%A8%E7%9B%AE%E7%9A%84%E5%9C%B0%E4%BA%8B%E4%B8%9A%E7%BE%A4'],
          statsName: 'toolbox_links_project_plans'),
      FeatureUnit(Strings.toolbox_rn_tools_title,
          icon: Icons.build,
          schemas: [
            combineQunarSchema(
                'flight/open?url=${Uri.encodeComponent(combineQunarSchema('react/open?hybridId=f_toolbox_rn'))}')
          ],
          statsName: 'toolbox_rntools'),
      FeatureUnit(Strings.toolbox_customer_service,
          icon: Icons.record_voice_over,
          schemas: [
            combineQTalkSchema(
                'qunarchat/openSingleChat?jid=meng.jiang@ejabhost1&realJid=meng.jiang@ejabhost1')
          ],
          statsName: 'toolbox_customer_support'),
      FeatureUnit('Test Entry', icon: Icons.ac_unit, clickHandler: () => navigateTo(
          context, Paths.pmoQuality,
          params: {
            'pmo': 'FLIGHT-106623'
          },
          hostMode: CompactHostMode.Inside))
    ]);
  }

  onClick(FeatureUnit unit) async {
    if (unit.clickHandler != null) {
      unit.clickHandler();
      return;
    }
    if (unit.schemas != null) {
      for (final schema in unit.schemas) {
        if (!await canLaunch(schema)) {
          Toast.toast(context, msg: '无法启动，请安装对应协议应用...$schema');
          continue;
        }
        await launch(schema);
      }
      return;
    }
    final router = Provider.of<Router>(context, listen: false);
    router.navigateTo(context, unit.routePath,
        transition: TransitionType.cupertinoFullScreenDialog);
  }

  @override
  onContextAttached(BuildContext context) async {
    super.onContextAttached(context);
  }
}

class FeatureUnit {
  String text;
  IconData icon;
  String routePath;
  List<String> schemas;
  String statsName;
  Widget customWidget;
  Function clickHandler;

  FeatureUnit(this.text,
      {@required this.icon,
      this.routePath,
      this.schemas,
      this.statsName,
      this.customWidget,
      this.clickHandler});
}
